package vtc;

import java.util.concurrent.*;
import java.io.*;


class ProcessingUnit implements Runnable 
{
	protected VtcArrayBlockingQueue inQueue_;
	protected VtcArrayBlockingQueue outQueue_;
	protected String id_;
	protected VtcTimeTaggedEvent inValue_;
	protected VtcTimeTaggedEvent outValue_;
	
	
	public ProcessingUnit() 
	{
		;
	}
	
	public ProcessingUnit(String id, VtcArrayBlockingQueue inQueue, VtcArrayBlockingQueue outQueue) 
	{
		id_ = id;
		inQueue_ = inQueue;
		outQueue_ = outQueue;
	}
	
	void preProcess() throws InterruptedException
	{
		;
	}
	
	void postProcess() throws InterruptedException
	{
		;
	}
	
	public void process() throws InterruptedException
	{
		outValue_ = inValue_;
	}
	
	public void run() 
	{
		try 
		{
			while(!Thread.interrupted()) 
			{
				this.preProcess();
				inValue_ = inQueue_.getQueue().take();
				Object currentObject = inValue_.getEvent();
				Double value = 0.0;
				Integer valuei = 0;
				//System.out.print("Class name " + currentObject.getClass().getName() + "\n");
				if(currentObject.getClass().getName() == "java.lang.Double")
				{
					value = (Double)currentObject;
				}
				if(currentObject.getClass().getName() == "java.lang.Integer")
				{
					valuei = (Integer)currentObject;
				}
				if(id_=="p0")
				{
					System.out.print("Process Unit id: " + id_ + " value: " + value+ "\n");
					//System.out.print("Process Unit id: " + id_ + " value: " + valuei+ "\n");
				}
				else if(id_=="p1")
				{
					System.out.print("Process Unit id: " + id_ + " value: " + value+ "\n");
					//System.out.print("Process Unit id: " + id_ + " value: " + valuei+ "\n");
				}
				else if(id_=="p2")
				{
					System.out.print("Process Unit id: " + id_ + " value: " + value+ "\n");
					//System.out.print("Process Unit id: " + id_ + " value: " + valuei+ "\n");
				}
				this.process();
				outQueue_.forcePut(outValue_);
				this.postProcess();
			}
		} 
		catch(InterruptedException e) 
		{
			System.out.print("ProcessUnit threat interrupted");
		}
		System.out.print("Exiting ProcessingUnit");
	}
}